local yiyan = fk.CreateSkill({
  name = "lingling__yiyan",
})

Fk:loadTranslationTable{
  ["lingling__yiyan"] = "易演",
  [":lingling__yiyan"] = "攻击范围内有你的角色回合开始时，你可以重铸一张黑色牌然后卜算2，或卜算2然后可以重铸一张红色牌。",

  ["lingling__yiyan1"] = "重铸一张黑色牌，然后卜算2",
  ["lingling__yiyan2"] = "卜算2，然后可以重铸一张红色牌",
  ["#lingling__yiyan-black"] = "易演：你可以重铸一张黑色牌，然后卜算2",
  ["#lingling__yiyan-red"] = "易演：你可以重铸一张红色牌",
}

yiyan:addEffect(fk.TurnStart, {
  anim_type = "control",
  can_trigger = function(self, event, target, player, data)
    return player:hasSkill(yiyan.name) and target:inMyAttackRange(player)
  end,
  on_cost = function(self, event, target, player, data)
    local room = player.room
    local all_choices = { "lingling__yiyan1", "lingling__yiyan2", "Cancel" }
    local choices = table.simpleClone(all_choices)
    if not table.find(player:getCardIds("he"), function (id)
      return Fk:getCardById(id).color == Card.Black
    end) then
      table.remove(choices, 1)
    end
    local choice = room:askToChoice(player, {
      choices = choices,
      skill_name = yiyan.name,
      all_choices = all_choices,
    })
    if choice ~= "Cancel" then
      if choice == "lingling__yiyan1" then
        local cards = room:askToCards(player, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          skill_name = yiyan.name,
          pattern = ".|.|spade,club",
          prompt = "#lingling__yiyan-black",
          cancelable = true,
        })
        if #cards > 0 then
          event:setCostData(self, {choice = choice, cards = cards})
          return true
        end
      else
        event:setCostData(self, {choice = choice})
        return true
      end
    end
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local card = event:getCostData(self).cards
    if card then
      room:recastCard(card, player, yiyan.name)
      if player.dead then return end
    end
    room:askToGuanxing(player, {
      cards = room:getNCards(2),
      skill_name = yiyan.name,
    })
    if card == nil and not player:isNude() then
      local cards = room:askToCards(player, {
        min_num = 1,
        max_num = 1,
        include_equip = true,
        skill_name = yiyan.name,
        pattern = ".|.|heart,diamond",
        prompt = "#lingling__yiyan-red",
        cancelable = true,
      })
      if #cards > 0 then
        room:recastCard(cards, player, yiyan.name)
      end
    end
  end,
})

return yiyan
